using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CardUI : MonoBehaviour
{
    private CardLibrarySO selectedCardLibrary;
    public Image chessBgImage;
    public Image chessImage;
    public TMP_Text hpCount;
    public TMP_Text soliderCount;
    public TMP_Text selectCount;
    public GameObject isSelected;
    
    public bool isClicked = false;
    public BuildingChessDateSO chessDate;
    
    void Start()
    {
        selectedCardLibrary = Resources.Load<CardLibrarySO>("GameDate\\CardLibrary\\selectedCardLibrary");
    }

    public void InitCard(BuildingChessDateSO date)
    {
        chessDate = date;
        chessBgImage.sprite = chessDate.chessBackgroundSprite;
        chessImage.sprite = chessDate.chessSprite;
        hpCount.text = chessDate.health.ToString();
        soliderCount.text = chessDate.soldierChessDates.Count.ToString();
    }

    public void CardIsClicked()
    {
        EventCenter.GetInstance().EventTrigger("ShowCardDetail",chessDate);
        if (isClicked)
        {
            isClicked = false;
            selectedCardLibrary.cardLibraryList.Remove(chessDate);
            //todo:更新UI
            EventCenter.GetInstance().EventTrigger("UpdateSelectedCard");
        }
        else
        {
            if (selectedCardLibrary.cardLibraryList.Count >= 5)
            {
                return;
            }
            else
            {
                selectedCardLibrary.cardLibraryList.Add(chessDate);
                isClicked = true;
                //todo:更新UI
                EventCenter.GetInstance().EventTrigger("UpdateSelectedCard");
            }
        }
    }

    public void UpdateSelectedCard()
    {
        if (isClicked)
        {
            isSelected.SetActive(true);
            int index = selectedCardLibrary.cardLibraryList.IndexOf(chessDate) + 1;
            selectCount.text = index.ToString();
        }
        else
        {
            isSelected.SetActive(false);
        }
    }
}
